package com.java.comprehensive.arithmetic.sort;

import java.util.*;

/**
 * @Author: Dgg
 * @Date: 2021/12/11 23:02
 */
public class EasySort {
	/**
	 * 三大基础排序
	 */
	public static void bubbleSort(int[] nums) {
		if (nums.length > 1) {

			for (int i = 0; i < nums.length; i++) {
				for (int j = 0; j < nums.length - 1; j++) {
					if (nums[j] > nums[j + 1]) {
						swap(nums, j, j + 1);
					}
				}
			}
		}
	}

	//插入
	public static void insertSort(int[] nums) {


		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = i + 1; j >= 1; j--) {
				if (nums[j] < nums[j - 1]) {
					swap(nums, j, j - 1);
				}
			}
		}
	}

	//选择排序
	public static void selectSort(int[] nums) {

		for (int i = 0; i < nums.length; i++) {
			int min = nums[i];
			int index = i;
			for (int j = i + 1; j < nums.length; j++) {
				if (min > nums[j]) {
					min = nums[j];
					index = j;
				}
			}
			swap(nums, index, i);
		}
	}

	public static void main(String[] args) {
		int[] nums = {1, 1, 0, 1, 1, 1};
		int maxConsecutiveOnesTwo = findMaxConsecutiveOnesTwo(nums);
		System.out.println(maxConsecutiveOnesTwo);
		System.out.println("结果：" + Arrays.toString(nums));
	}

	public static int findMaxConsecutiveOnes(int[] nums) {
		if (nums.length < 1) {
			return 0;
		}
		List<Integer> list = new ArrayList<>();
		int temp = 0;
		for (int num : nums) {
			if (num == 1) {
				temp++;
			} else {
				list.add(temp);
				temp = 0;
			}
		}
		list.add(temp);
		Collections.sort(list);
		return list.get(list.size() - 1);
	}


	public static int findMaxConsecutiveOnesTwo(int[] nums) {
		if (nums.length < 1) {
			return 0;
		}
		int k1 = 0, k2 = 0;
		for (int i = 0; i < nums.length; i++) {
			if (nums[i] == 1) {
				k1++;
			} else {
				if (k1 > k2) {
					k2 = k1;
				}
				k1 = 0;
			}
		}
		return Math.max(k1, k2);
	}

	public static void swap(int[] nums, int i, int j) {
		int temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}
}
